package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.PageBean;
import com.itheima.dto.QueryStudentDto;
import com.itheima.mapper.StudentMapper;
import com.itheima.pojo.Student;
import com.itheima.service.StudentService;
import com.itheima.vo.ClazzCountVo;
import com.itheima.vo.StudentVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentMapper studentMapper;

    @Override
    public void save(Student student) {
        student.setCreateTime(LocalDateTime.now());
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.insert(student);
    }


    @Override
    public PageBean page(QueryStudentDto dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        Page<StudentVo> p = studentMapper.list(dto);
        return new PageBean(p.getTotal(), p.getResult());
    }


    @Override
    public Student getInfo(Integer id) {
        return studentMapper.getById(id);
    }


    @Override
    public void update(Student student) {
        student.setUpdateTime(LocalDateTime.now());
        studentMapper.update(student);
    }


    @Override
    public void delete(List<Integer> ids) {
        ids.forEach(id -> {
            studentMapper.deleteById(id);
        });
    }


    @Override
    public void violationHandle(Integer id, Integer score) {
        studentMapper.updateViolation(id, score);
    }

    /**
     * 学员学历统计
     *
     * @return
     */
    @Override
    public List<Map> getStudentDegreeData() {
        return studentMapper.getStudentDegreeData();
    }

    /**
     * 班级人数统计
     *
     * @return
     */
    @Override
    public ClazzCountVo getStudentCountData() {
        List<Map> countList = studentMapper.getStudentCount();
        List<Object> clazzList = countList.stream().map(map -> map.get("cname")).collect(Collectors.toList());
        List<Object> dataList = countList.stream().map(map -> map.get("scount")).collect(Collectors.toList());
        return new ClazzCountVo(clazzList, dataList);
    }
}
